﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using DataContracts;
using System.ServiceModel;

namespace TrainingWorkshop.MVPWebsite.Role.Views
{
    public class BatchUpdateRoleStatusPresenter : Presenter<IBatchUpdateRoleStatusView>
    {
        private IRoleController _controller;

        public enum RoleStatus
        {
            InUse = 1,
            NoUse = 2
        }
        public BatchUpdateRoleStatusPresenter([CreateNew] IRoleController controller)
        {
            _controller = controller;
        }


        /// <summary>
        /// GetAllRole
        /// </summary>
        /// <returns></returns>
        public void GetAllRole(string roleName, int status)
        {
            string name = View.StatusName;
            View.RoleDetailsList = _controller.GetAllRole(View.RoleName, View.StatusId);
         }
        /// <summary>
        /// Delete Role
        /// </summary>
        public void DeleteRole(int roleId)
        {
            try
            {
                _controller.DeleteRole(roleId);

            }
            catch (FaultException ex)
            {
                View.ShowOperationMessage(ex.Message);
            }
        }

        public void UpdateCheck(int roleId)
        {
            try
            {
                _controller.GetRoleById(roleId);
                if (_controller.GetRoleById(roleId) != null)
                {
                    View.Redirect("RoleDetail.aspx?roleid_trans=" + roleId);
                   
                }
            }
            catch (FaultException ex)
            {
                View.ShowOperationMessage(ex.Message);
               
               
            }
        }

     


        public void GetAllRoleStatus()
        {
            View.StatusList = _controller.GetAllRoleStatus();
        }


        public void BatchInUse()
        {
            List<int> list = View.Selectroleid;
            List<RoleDTO> roles = new List<RoleDTO>();
            for (int i = 0; i < list.Count; i++)
            {
                RoleDTO role = new RoleDTO();
                StatusDTO status = new StatusDTO();

                role.Status = status;
                role.RoleId = list[i];
                role.Status.StatusId = (int)RoleStatus.InUse;
                roles.Add(role);
            }

            _controller.BatchUpdateRoleStatus(roles);

        }
        public void BatchNoUse()
        {
            List<int> list = View.Selectroleid;
            List<RoleDTO> roles = new List<RoleDTO>();
            for (int i = 0; i < list.Count; i++)
            {
                RoleDTO role = new RoleDTO();
                StatusDTO status = new StatusDTO();
                role.Status = status;
                role.RoleId = list[i];

                role.Status.StatusId = (int)RoleStatus.NoUse;
                roles.Add(role);
            }

            _controller.BatchUpdateRoleStatus(roles);

        }


        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            GetAllRoleStatus();
        }

        // TODO: Handle other view events and set state in the view
    }
}



